package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.dao.TableAreaDao;
import org.example.dao.TableInfoDao;
import org.example.pojo.TableArea;
import org.example.pojo.TableInfo;
import org.example.service.TableAreaService;
import org.example.service.TableInfoService;
import org.example.uitl.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TableInfoImpl extends ServiceImpl<TableInfoDao, TableInfo> implements TableInfoService {

    @Autowired
    private TableAreaDao tableAreaDao;

    @Autowired
    private TableInfoDao tableInfoDao;

    @Override
    public Page<TableInfo> FindTableArea(Query query) {
        Page<TableInfo> page = new Page<>(query.getPage(), query.getSize());
        if (query.getName()==null){
            tableInfoDao.selectPage(page, null);
            List<TableInfo> list = page.getRecords();
            if (list.size()>0){
                for (TableInfo t : list) {
                    Integer areaId = t.getAreaId();
                    System.out.println("areaId:"+areaId);
                    QueryWrapper<TableArea> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("area_id",areaId);
                    TableArea tableArea = tableAreaDao.selectOne(queryWrapper);
                    if (tableArea != null) {
                        t.setAreaName(tableArea.getAreaName());
                        t.setShuoming(tableArea.getShuoming());
                    } else {
                        t.setAreaName("nulll");
                        t.setShuoming("nulll");
                    }
                }
            }
            return page;
        }
//        else {
//            QueryWrapper<TableArea> tableAreaQueryWrapper = new QueryWrapper<>();
//            tableAreaQueryWrapper.like("area_name",query.getName());
//            List<TableArea> tableAreas = tableAreaDao.selectList(tableAreaQueryWrapper);
//            if (tableAreas.size()>0){
//                tableInfoDao.selectPage(page, null);
//                List<TableInfo> list = page.getRecords();
//                if (list.size() > 0){
//                    for (TableInfo t : list) {
//                        Integer areaId = t.getAreaId();
//                        for (TableArea tarea : tableAreas) {
//                            if (areaId == tarea.getAreaId()){
//                                t.setAreaName(tarea.getAreaName());
//                                t.setShuoming(tarea.getShuoming());
//                            }
//                        }
//                    }
//                }
//            }else{
//                return null;
//            }
//        }
        return page;
    }

    @Override
    public Page<TableInfo> tablelist(Query query) {
        Page<TableInfo> page = new Page<>(query.getPage(), query.getSize());
        QueryWrapper<TableInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(query.getArea() != null,"area_id",query.getArea());
        tableInfoDao.selectPage(page, queryWrapper);
        List<TableInfo> list = page.getRecords();
        if (list.size()>0){
            for (TableInfo t : list) {
                Integer areaId = t.getAreaId();
                TableArea tableArea = tableAreaDao.selectById(areaId);
                if (tableArea != null) {
                    t.setAreaName(tableArea.getAreaName());
                    t.setShuoming(tableArea.getShuoming());
                } else {
                    t.setAreaName("nulll");
                    t.setShuoming("nulll");
                }
            }
        }
        return page;
    }
}